package com.sskj.level.http;


import com.lzy.okgo.OkGo;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.common.util.SPUtil;
import com.sskj.level.bean.ContractNoteBean;
import com.sskj.level.bean.ProductDataBean;
import com.sskj.level.bean.SpreadBean;
import com.sskj.level.bean.TradeInfoBean;
import com.sskj.level.bean.TradeListBean;
import com.sskj.lib.SPConfig;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.bean.WSFiveBean;
import com.sskj.lib.util.CommonUtil;

import java.util.List;

import okhttp3.WebSocket;

/**
 * 作者 :吕志豪
 * 简书：https://www.jianshu.com/u/6e525b929aac
 * github：https://github.com/lvzhihao100
 * 描述：
 * 创建时间：2018-10-15 14:08
 */
public class HttpService {
    public GetRequest<HttpData<WSFiveBean>> getDeepData(String code) {
        return OkGo.<HttpData<WSFiveBean>>get(HttpConfig.BASE_URL + HttpConfig.GET_DEEP)
                .params("code", CommonUtil.dealReuqestCode(code))
                ;
    }

    public WebSocketObserver pushCoinFiveDeep(String code) {
        return new WebSocketObserver(HttpConfig.WS_BASE_URL) {
            @Override
            public void onOpen(WebSocket webSocket, okhttp3.Response response, String msg) {
                if (msg == null) {
                    webSocket.send("{\"type\":\"depth_list\",\"list\":[\"" + code + "\"]}");
                } else {
                    webSocket.send(msg);
                }
            }
        };
    }

    /**
     * 首页行情列表
     *
     * @param code 标识符
     * @return
     */
    public GetRequest<HttpData<List<CoinBean>>> getPro(String code) {
        return OkGo.<HttpData<List<CoinBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_PRO)
                .params("code", CommonUtil.dealReuqestCode(code))
                ;
    }

    public GetRequest<HttpData<ProductDataBean>> getProductData(String code) {
        return OkGo.<HttpData<ProductDataBean>>get(HttpConfig.BASE_URL + HttpConfig.PRODUCT_DATA)
                .params("stockCode", CommonUtil.dealReuqestCode(code));
    }


    /**
     * 盘口
     *
     * @param code 币种标识符
     * @return
     */
    public GetRequest<HttpData<List<WSFiveBean>>> getPankou(String code) {
        return OkGo.<HttpData<List<WSFiveBean>>>get(HttpConfig.BASE_URL + HttpConfig.GET_PANKOU)
                .params("code", CommonUtil.dealReuqestCode(code))
                ;
    }


    public GetRequest<HttpData<TradeInfoBean>> getTradeInfo() {
        return OkGo.<HttpData<TradeInfoBean>>get(HttpConfig.BASE_URL + HttpConfig.GET_TRADE_INFO)
                .params("stockUserId", SPUtil.get(SPConfig.ID, ""))
                ;
    }

    /**
     * 一键平仓
     *
     * @return
     */
    public PostRequest<HttpData> levelAllPc(String stockCode) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.LEVEL_ALL_PC)
                .params("stockUserId", SPUtil.get(SPConfig.ID, ""))
                .params("stockCode", stockCode)
                ;
    }


    public GetRequest<HttpData<PageBean<TradeListBean>>> getChiCang(String page, String pageSize,String stockCode) {
        return OkGo.<HttpData<PageBean<TradeListBean>>>get(HttpConfig.BASE_URL + HttpConfig.CHICANG_LIST)
                .params("stockUserId", SPUtil.get(SPConfig.ID, ""))
                .params("pageNumber", page)
                .params("pageSize", pageSize)
                .params("stockCode", stockCode)
                ;
    }

    /**
     * 平仓
     *
     * @param orderId
     * @return
     */
    public PostRequest<HttpData> pingCang(String orderId,String num) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.PINGCANG)
                .params("stockUserId", SPUtil.get(SPConfig.ID, ""))
                .params("orderId", orderId)
                .params("entrustLot", num)
                ;
    }


    public GetRequest<HttpData<PageBean<TradeListBean>>> getWeituo(String stockCode, String pageNum, String pageSize) {
        return OkGo.<HttpData<PageBean<TradeListBean>>>get(HttpConfig.BASE_URL + HttpConfig.WEITUO)
                .params("stockUserId", SPUtil.get(SPConfig.ID, ""))
                .params("pageNumber", pageNum)
                .params("stockCode", stockCode)

                .params("pageSize", pageSize)
                ;
    }

    public GetRequest<HttpData<PageBean<ContractNoteBean>>> getChengjiao(String page, String pageSize,String stockCode) {
        return OkGo.<HttpData<PageBean<ContractNoteBean>>>get(HttpConfig.BASE_URL + HttpConfig.CHENGJIAO)
                .params("pageNumber", page)
                .params("pageSize", pageSize)
                .params("stockCode", stockCode)
                .params("stockUserId", SPUtil.get(SPConfig.ID, ""))
                ;
    }


    public PostRequest<HttpData> setStop(String orderId, String stopProfitPrice, String stopLossPrice) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SET_POIT)
                .params("stockUserId", SPUtil.get(SPConfig.ID, ""))
                .params("orderId", orderId)
                .params("stopProPrice", stopProfitPrice)
                .params("stopLossPrice", stopLossPrice)
                ;
    }

    /**
     * 撤单
     *
     * @param orderId
     * @return
     */
    public PostRequest<HttpData> cancelOrder(String orderId) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.CANCEL_ORDER)
                .params("stockUserId", SPUtil.get(SPConfig.ID, ""))
                .params("orderId", orderId)
                ;
    }


    public WebSocketObserver pushCoinFive(String code) {
        return new WebSocketObserver(HttpConfig.WS_BASE_URL) {

            public void onOpen(WebSocket webSocket, okhttp3.Response response, String msg) {
                if (msg == null) {
                    webSocket.send("{\"type\":\"buy_sell_five_lever\",\"list\":[\"" + code + "\"]}");
                } else {
                    webSocket.send(msg);
                }
            }
        };
    }


    /**
     * 下单
     */
    public PostRequest<HttpData> createOrder(String entrustLot, String buyPrice, String buyBillType, String stockCode, String billPriceType, String stopProfit, String stopLoss, String lever) {
        return OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.CREATE_ORDER)
                .params("stockUserId", SPUtil.get(SPConfig.ID, ""))
                .params("buyPrice", buyPrice)
                .params("billPriceType", billPriceType)
                .params("buyBillType", buyBillType)
                .params("entrustLot", entrustLot)
                .params("stockCode", CommonUtil.dealReuqestCode(stockCode))
                .params("lever", lever)
                ;
    }


    public GetRequest<HttpData<SpreadBean>> apiUserRegisterlink() {
        return OkGo.<HttpData<SpreadBean>>get(HttpConfig.BASE_URL + HttpConfig.API_USER_REGISTERLINK)
                ;
    }
}